package com.caiwangdai.app.origin.list

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.caiwangdai.app.extension.inflate
import com.caiwangdai.app.extension.setOnClickListenerPro
import com.caiwangdai.app.origin.list.holder.BaseRecyclerViewHolder

/**
 * @project KotLinProject
 * @author 艹羊
 * @date 2017/6/15 下午5:38
 * @description 适配器的基类
 */
abstract class BaseRecyclerAdapter<T> : RecyclerView.Adapter<BaseRecyclerViewHolder> {
    val TYPE_HEADER = 0
    val TYPE_ITEM = 1
    val TYPE_FOOT = 2


    var mContext: Context
    var dataList = mutableListOf<T>()

    var onItemClick: ((position: Int) -> Unit)? = null

    var headView: View? = null
    var footView: View? = null
    var headViewSize: Int = 0
    var footViewSize: Int = 0
    var isAddHead = false
    var isAddFoot = false


    constructor(mContext: Context) : super() {
        this.mContext = mContext
    }


    //获取Viewholder
    abstract fun getViewHolder(picNewsView: View?): BaseRecyclerViewHolder


    //获取布局
    abstract fun getItemLayoutRes(): Int

    //视图变化
    abstract fun ItemViewChange(holder: BaseRecyclerViewHolder?, position: Int)

    /**
     * 增加头部视图
     */
    fun addHeadView(view: View) {
        headView = view
        headViewSize = 1
        isAddHead = true
    }

    /**
     * 增加底部视图
     */
    fun addFootView(view: View) {
        footView = view
        footViewSize = 1
        isAddFoot = true
    }

    /**
     * 去除头部视图
     */
    fun removeHeadView() {
        headView = null
        headViewSize = 0
        isAddHead = false
    }


    /**
     * 去除底部视图
     */
    fun removeFootView() {
        footView = null
        footViewSize = 0
        isAddFoot = false
    }

    override fun getItemViewType(position: Int): Int {
        var type = TYPE_ITEM
        if (headViewSize == 1 && position == 0) {
            type = TYPE_HEADER
        } else if (footViewSize == 1 && position == itemCount - 1) {
            //最后一个位置
            type = TYPE_FOOT
        }
        return type
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseRecyclerViewHolder {
        var targetView: View? = null
        val mInflater = LayoutInflater.from(parent?.context)
        when (viewType) {
            TYPE_HEADER -> {
                return BaseRecyclerViewHolder(headView)
            }
            TYPE_FOOT -> {
                return BaseRecyclerViewHolder(footView)
            }
            TYPE_ITEM -> {
                targetView = parent?.inflate(getItemLayoutRes())
                return getViewHolder(targetView)
            }
        }
        return getViewHolder(targetView)
    }

    override fun onBindViewHolder(holder: BaseRecyclerViewHolder?, position: Int) {
        when (getItemViewType(position)) {
            TYPE_ITEM -> {
                holder?.itemView?.setOnClickListenerPro {
                    onItemClick?.invoke(position)
                }
                ItemViewChange(holder, position)
            }
        }
    }


    override fun getItemCount(): Int = dataList.size + headViewSize + footViewSize

    open fun getDataListCount(): Int = dataList.size


    open fun setDataToAdapter(data: List<T>?) {
        dataList.clear()
        addData(data)
    }


    open fun addData(data: List<T>?) {
        if (data == null) return
        dataList.addAll(data)
        val start = itemCount - 1
        val count = data.size
        notifyItemRangeChanged(start, count)
    }

    open fun getItem(position: Int): T? {
        if (dataList.size == 0) return null
        return dataList[position - headViewSize]
    }


    fun setOnItemClickListener(onItemClick: (position: Int) -> Unit) {
        this.onItemClick = onItemClick
    }
}